import java.util.ArrayList;
import java.util.List;

/**
 * Creat with IntelliJ IDEA
 * Description:二维网格迁移
 * User:WZW
 * Date:2021-12-04
 * Time:10:28
 */
class Solution {
    public static List<List<Integer>> shiftGrid(int[][] grid, int k) {
        List<List<Integer>> list = new ArrayList<>();

        for (int i = 0; i < grid.length; i++) {
            List<Integer> l = new ArrayList<>();

            for (int j = 0; j < grid[0].length; j++) {
                l.add(grid[i][j]);
            }
            list.add(l);
        }

        while(k>0){
            int tmp = list.get(grid.length-1).get(grid[0].length-1);
            for (int i = grid.length-1; i >= 0 ; i--) {
                for (int j = grid[0].length-1; j >=0 ; j--) {
                    if(j==0){
                        if(i==0){
                            list.get(i).set(j,tmp);
                        }else {
                            list.get(i).set(j,list.get(i-1).get(grid[0].length-1));
                        }
                    }else {
                        list.get(i).set(j,list.get(i).get(j-1));
                    }
                }
            }
            k--;
        }
        return list;
    }
}

public class homework_2021_12_4_Ledcode2 {
    public static void main(String[] args) {
        int[][] grid = new int[][]{
                {1,2,3},{4,5,6},{7,8,9}
        };
        System.out.println(Solution.shiftGrid(grid, 3));
    }
}
